﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CMS_GYM
{
    public partial class frmTimKiemPhieuGiaoDich : Form
    {
        #region Initial
        public frmTimKiemPhieuGiaoDich()
        {
            InitializeComponent();
        }

        private void frmTimKiemPhieuGiaoDich_Load(object sender, EventArgs e)
        {
            try
            {
                KhoiTao();
            }
            catch (Exception ex)
            { }
        }

        #endregion

        #region events
        private void tbnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime? TuNgay = txtdTuNgay.value == new DateTime() ? null : (DateTime?)txtdTuNgay.value;
                DateTime? DenNgay = txtDenNgay.value == new DateTime() ? null : (DateTime?)txtDenNgay.value;
                string TuSo = string.IsNullOrEmpty(txtTuSo.Text) || txtTuSo.Text.Trim() == "" ? string.Empty : txtTuSo.Text;
                string DenSo = string.IsNullOrEmpty(txtDenSo.Text) || txtDenSo.Text.Trim() == "" ? string.Empty : txtDenSo.Text;
                string KyHieu = string.IsNullOrEmpty(txtKyHieu.Text) || txtKyHieu.Text.Trim() == "" ? string.Empty : txtKyHieu.Text;
                int? NguoiTaoId = cbNhanVien.SelectedValue == null || string.IsNullOrEmpty(cbNhanVien.SelectedValue.ToString()) ? null : (int?)int.Parse(cbNhanVien.SelectedValue.ToString());
                string MaKhachHang = null;
                string TuKhoa = string.IsNullOrEmpty(txtTuKhoa.Text) || txtTuKhoa.Text.Trim() == "" || txtTuKhoa.Text == "Tìm kiếm theo: Mã KH, Tên KH,Địa chỉ " ? string.Empty : txtTuKhoa.Text;
                string LoaiPhieu = cbLoaiPhieu.SelectedValue == null || string.IsNullOrEmpty(cbLoaiPhieu.SelectedValue.ToString()) ? null : cbLoaiPhieu.SelectedValue.ToString();
                gvPhieuGiaoDich.DataSource = new clsPhieuThuTienBUS().TimKiemPhieuGiaoDich(TuNgay, DenNgay, TuSo, DenSo, KyHieu, NguoiTaoId, MaKhachHang, TuKhoa, LoaiPhieu);
                grbKetQuaTim.Text = "Kết quả tìm kiếm: " + gvPhieuGiaoDich.RowCount.ToString() ;
            }
            catch(Exception)
            {
            }
        }

        private void gvPhieuGiaoDich_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (gvPhieuGiaoDich.Rows.Count > 0 && e.RowIndex >= 0)
                {
                    string MaLoaiPhieu = gvPhieuGiaoDich.Rows[e.RowIndex].Cells["MaLoaiPhieu"].Value.ToString().Trim();
                    string SoPhieu = gvPhieuGiaoDich.Rows[e.RowIndex].Cells["SoPhieu"].Value.ToString();
                    string KyHieu = gvPhieuGiaoDich.Rows[e.RowIndex].Cells["KyHieu"].Value.ToString();
                    if (MaLoaiPhieu == "1")
                    {
                        frmThongTinPhieuThu frm = new frmThongTinPhieuThu(SoPhieu, KyHieu);
                        frm.ShowDialog();
                        //Kiem tra co thao tac luu hay xoa phieu thu khong
                        //if (frm.IsThaoTac)
                        //{
                        //    tbnTimKiem_Click(null, null);
                        //}
                        tbnTimKiem_Click(null, null);
                    }
                    else
                    {
                        frmThongTinPhieuChi frm = new frmThongTinPhieuChi(SoPhieu, KyHieu);
                        frm.ShowDialog();
                        //Kiem tra co thao tac luu hay xoa phieu thu khong
                        //if (frm.IsThaoTac)
                        //{
                        //    tbnTimKiem_Click(null, null);
                        //}
                        tbnTimKiem_Click(null, null);
                    }

                }
            }
            catch (Exception)
            { }
        }


        #endregion

        #region Functions
        private void KhoiTao()
        {
            gvPhieuGiaoDich.AutoGenerateColumns = false;
            txtdTuNgay.value = DateTime.Now;
            txtDenNgay.value = DateTime.Now;
            LoadComboBoxLoaiPhieu();
            LoadComboBoxNhanVien(); 
        }

        private void LoadComboBoxNhanVien()
        {
            DataTable table = new clsNhanVienBUS().LayBang();
            object[] M = new object[] { null, null, null, null, null, null, null, null, null, null, null, null };
            table.Rows.Add(M);
            DataView view = table.DefaultView;
            view.Sort = "TenNhanVien";
            cbNhanVien.DataSource = view;
            cbNhanVien.DisplayMember = "TenNhanVien";
            cbNhanVien.ValueMember = "NhanVienId";
        }

        private void LoadComboBoxLoaiPhieu()
        {
            DataTable table = new DataTable();
            table.Columns.Add("MaLoaiPhieu");
            table.Columns.Add("TenLoaiPhieu");
            object[] M = new object[] { "PhieuThu", "Phiếu thu" };
            table.Rows.Add(M);
            M = new object[] { "PhieuChi", "Phiếu chi" };
            table.Rows.Add(M);
            M = new object[] { "TatCa", "Tất cả" };
            table.Rows.Add(M);
            cbLoaiPhieu.DataSource = table;
            cbLoaiPhieu.DisplayMember = "TenLoaiPhieu";
            cbLoaiPhieu.ValueMember = "MaLoaiPhieu";
        }

        #endregion

        
    }
}
